This script checks if the set Visited Document exists and puts the current document into it.
ON Open OF ANY Document;
VAR aSet : Set;
BEGIN
aSet := GetNamedObject('Visited Documents');
IF aSet = nullObject THEN
BEGIN
aSet := NewSet;
aSet.SetName('Visited Documents')
END;
aSet.AddObject(current)
END
3 Suggesting a Partial Ordering on Document Opening
This script asks the user whether he/she wants to study the document SimpleExample before the current one.
ON Open OF Document [ComplexExample];
BEGIN
IF NOT ([SimpleExample] IN [Visited Documents]) THEN
IF ReadOk('Study a simpler example first?') THEN
BEGIN
[SimpleExample].Open;
Abort
END
END
4 Selectively Closing Documents
This script closes all the documents attached to the current document, unless they belong to the set KeepOpenDocs.
ON Close OF ANY Document IN [AnchorDocuments];
VAR doc : Document;
BEGIN
FOR EACH doc IN current.GetAttachedDocuments DO
IF NOT (doc IN [KeepOpenDocs]) THEN doc.Close;
END
5 Formatting a Text Document
This script applies several formatting commands to the current selection in a textual document (i.e. to obtain the format of the title above). To use it, select the text to format, choose the Execute Other Script command from the Script menu and execute the script.
ON EXECUTE;
BEGIN
DoMenu('Times');
DoMenu('18 Point');
DoMenu('Underline');
END
6 Creating a Simple Browser
This script creates a browser in the same directory as the application, which contains all the graphical documents in the set Visited Documents whose name starts with “Example”.
ON Execute;
VAR doc1: BrowserDocument;
doc2: PictDocument;
BEGIN
DoMenu('New Browser Document…');
doc1 := AsUNIVObject(frontDocument); { frontDocument is of class Document, so we have to make it compatible with the class of doc1, i.e. BrowserDocument. Since we just created a browser document, this should not cause any problems.}
FOR EACH doc2 IN [Visited Documents] DO
IF Pos('Example',doc2.GetName) = 1 THEN
BEGIN { "Example" was found at the beginning of the document name }
doc1.CreateNode(doc2.GetName);
doc1.AddDocument(doc2.GetName,doc2)
END;
SetDirectory(':');
doc1.Save('Example Graphic Documents')
END
7 Using a Guided Tour
This script activates a browser and opens the documents associated with its nodes, asking the user after each visited node whether he/she wants to continue. Note that we use the variable theBrowser to store a reference to the browser, instead of using each time the object specifier [Demo.Table] which would force the system to search repeatedly for the same document within all the objects in the open webs, a time-consuming task.
ON Execute;
VAR theBrowser: BrowserDocument;
continue: Integer;
BEGIN
theBrowser := [Demo.Table];
theBrowser.Activate;
theBrowser.DeselectAll;
continue := true;
WHILE continue AND IsAvailable('Next Node') DO
BEGIN
DoMenu('Next Node');
continue := ReadOk('Press OK to continue, Cancel to abort.');
END
END
8 Searching and Replacing Strings in a Document
This script will replace all occurrences of the string s1 in the document doc with s2.
ON Execute(doc: TextDocument;s1,s2: String);
VAR index: Integer;
BEGIN
doc.Select; { makes the document the active one }
doc.DeselectAll; { puts the cursor at the beginning of the document }
index := doc.FindString(s1,false);
WHILE index > 0 DO
BEGIN
doc.Selectstring(index,Length(s1));
doc.Insertstring(s2);
index := doc.FindString(s1,false)
END
END
A possible use of the above script (supposedly named ReplaceAll) would be:
textDoc.SelectString(index+Length(theString),0); { to advance the cursor, so that we don't find the same word again and again }
index := textDoc.FindString(theString,false);
END;
END
END;
{3}FOR EACH textDoc IN theSet DO textDoc.Close { to close all the documents in theSet }
END
To use this script, create a glossary (textual) document, create blocks for the entries in the glossary document (e.g. by selecting each word and choosing the "Create Block" command from the WEBSs menu), and give them the type Glossary (do not forget to create the type Glossary before trying to compile the script). Put all the documents to be searched in a set, and put the line below in a script to call the above script (supposedly named "CreateGlossary") :